﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RM2012Server.Business;

namespace RM2012Server.Model
{
    public class InvoiceDAO
    {
        DataServices m_InvoiceDAO = new DataServices();

        public DataTable GetAll()
        {
            m_InvoiceDAO.Load("GetAllInvoice", null);
            return m_InvoiceDAO;
        }

        public DataTable GetAllFull()
        {
            m_InvoiceDAO.Load("GetAllInvoiceFull", null);
            return m_InvoiceDAO;
        }

        public DataTable GetById(String invoiceId)
        {
            SqlParameter param = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param.Value = invoiceId;

            m_InvoiceDAO.Load("GetInvoiceById", param);
            return m_InvoiceDAO;
        }

        public DataTable GetByIdForReport(String invoiceId)
        {
            SqlParameter param = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param.Value = invoiceId;

            m_InvoiceDAO.Load("GetInvoiceByIdForReport", param);
            return m_InvoiceDAO;
        }

        public DataTable GetByLikeId(String invoiceId)
        {
            SqlParameter param = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param.Value = invoiceId;

            m_InvoiceDAO.Load("GetInvoiceByLikeId", param);
            return m_InvoiceDAO;
        }

        public DataTable GetByCusId(String cusId)
        {
            SqlParameter param = new SqlParameter("@CusId", SqlDbType.VarChar, 6);
            param.Value = cusId;

            m_InvoiceDAO.Load("GetInvoiceByCusId", param);
            return m_InvoiceDAO;
        }

        public DataTable GetByTablesId(String tablesId)
        {
            SqlParameter param = new SqlParameter("@TablesId", SqlDbType.VarChar, 6);
            param.Value = tablesId;

            m_InvoiceDAO.Load("GetInvoiceByTablesId", param);
            return m_InvoiceDAO;
        }

        public DataTable GetByIsPaid(bool isPaid)
        {
            SqlParameter param = new SqlParameter("@IsPaid", SqlDbType.Bit);
            param.Value = isPaid;

            m_InvoiceDAO.Load("GetInvoiceIsPaid", param);
            return m_InvoiceDAO;
        }

        public DataTable GetByIsDeleted(bool isDeleted)
        {
            SqlParameter param = new SqlParameter("@IsDeleted", SqlDbType.Bit);
            param.Value = isDeleted;

            m_InvoiceDAO.Load("GetInvoiceIsDeleted", param);
            return m_InvoiceDAO;
        }

        public DataTable GetByYear(int year)
        {
            SqlParameter param = new SqlParameter("@Year", SqlDbType.Int);
            param.Value = year;

            m_InvoiceDAO.Load("GetInvoiceYear", param);
            return m_InvoiceDAO;
        }

        public DataTable GetByMonth(int year, int month)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@Year", SqlDbType.Int);
            param[0].Value = year;

            param[1] = new SqlParameter("@Month", SqlDbType.Int);
            param[1].Value = month;

            m_InvoiceDAO.Load("GetInvoiceMonth", param);
            return m_InvoiceDAO;
        }

        public DataTable GetByTotal(double min, double max)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@Min", SqlDbType.Money);
            param[0].Value = min;

            param[1] = new SqlParameter("@Max", SqlDbType.Money);
            param[1].Value = max;

            m_InvoiceDAO.Load("GetInvoiceByTotal", param);
            return m_InvoiceDAO;
        }

        public DataTable GetByDate(DateTime start, DateTime end)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@Start", SqlDbType.DateTime);
            param[0].Value = start;

            param[1] = new SqlParameter("@Max", SqlDbType.DateTime);
            param[1].Value = end;

            m_InvoiceDAO.Load("GetInvoiceByDate", param);
            return m_InvoiceDAO;
        }

        public DataTable GetByDateFull(DateTime dateChoise)
        {
            SqlParameter param = new SqlParameter("@DateChoise", SqlDbType.DateTime);
            param.Value = dateChoise;

            m_InvoiceDAO.Load("GetInvoiceByDateFull", param);
            return m_InvoiceDAO;
        }

        public DataTable GetToday()
        {
            m_InvoiceDAO.Load("GetInvoiceToday", null);
            return m_InvoiceDAO;
        }

        public int Add(InvoiceInfo info)
        {
            SqlParameter[] param = new SqlParameter[10];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = info.InvoiceId;

            param[1] = new SqlParameter("@InvoiceDate", SqlDbType.DateTime);
            param[1].Value = info.InvoiceDate;

            param[2] = new SqlParameter("@CusId", SqlDbType.VarChar, 6);
            param[2].Value = info.CusId;

            param[3] = new SqlParameter("@TablesId", SqlDbType.VarChar, 6);
            param[3].Value = info.TablesId;

            param[4] = new SqlParameter("@Total", SqlDbType.Money);
            param[4].Value = info.Total;

            param[5] = new SqlParameter("@Paid", SqlDbType.Money);
            param[5].Value = info.Paid;

            param[6] = new SqlParameter("@IsDeleted", SqlDbType.Bit);
            param[6].Value = info.IsDeleted;

            param[7] = new SqlParameter("@IsPaid", SqlDbType.Bit);
            param[7].Value = info.IsPaid;

            param[8] = new SqlParameter("@DiscountPer", SqlDbType.Money);
            param[8].Value = info.DiscountPer;

            param[9] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[9].Value = info.Remark;

            return m_InvoiceDAO.ExecuteUpdate("AddInvoice", param);
        }

        public int Update(InvoiceInfo info)
        {
            SqlParameter[] param = new SqlParameter[10];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = info.InvoiceId;

            param[1] = new SqlParameter("@InvoiceDate", SqlDbType.DateTime);
            param[1].Value = info.InvoiceDate;

            param[2] = new SqlParameter("@CusId", SqlDbType.VarChar, 6);
            param[2].Value = info.CusId;

            param[3] = new SqlParameter("@TablesId", SqlDbType.VarChar, 6);
            param[3].Value = info.TablesId;

            param[4] = new SqlParameter("@Total", SqlDbType.Money);
            param[4].Value = info.Total;

            param[5] = new SqlParameter("@Paid", SqlDbType.Money);
            param[5].Value = info.Paid;

            param[6] = new SqlParameter("@IsDeleted", SqlDbType.Bit);
            param[6].Value = info.IsDeleted;

            param[7] = new SqlParameter("@IsPaid", SqlDbType.Bit);
            param[7].Value = info.IsPaid;

            param[8] = new SqlParameter("@DiscountPer", SqlDbType.Money);
            param[8].Value = info.DiscountPer;

            param[9] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[9].Value = info.Remark;

            return m_InvoiceDAO.ExecuteUpdate("UpdateInvoice", param);
        }

        public int UpdateTotal(InvoiceInfo info)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = info.InvoiceId;

            param[1] = new SqlParameter("@Total", SqlDbType.Money);
            param[1].Value = info.Total;

            return m_InvoiceDAO.ExecuteUpdate("UpdateInvoiceTotal", param);
        }

        public int UpdatePaid(InvoiceInfo info)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = info.InvoiceId;

            param[1] = new SqlParameter("@Paid", SqlDbType.Money);
            param[1].Value = info.Paid;

            param[2] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[2].Value = info.Remark;

            return m_InvoiceDAO.ExecuteUpdate("UpdateInvoicePaid", param);
        }

        public int UpdateNeeded(InvoiceInfo info)
        {
            SqlParameter[] param = new SqlParameter[6];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = info.InvoiceId;

            param[1] = new SqlParameter("@Paid", SqlDbType.Money);
            param[1].Value = info.Paid;

            param[2] = new SqlParameter("@IsDeleted", SqlDbType.Bit);
            param[2].Value = info.IsDeleted;

            param[3] = new SqlParameter("@IsPaid", SqlDbType.Bit);
            param[3].Value = info.IsPaid;

            param[4] = new SqlParameter("@DiscountPer", SqlDbType.Money);
            param[4].Value = info.DiscountPer;

            param[5] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[5].Value = info.Remark;

            return m_InvoiceDAO.ExecuteUpdate("UpdateInvoiceNeeded", param);
        }

        public int Delete(String invoiceId)
        {
            SqlParameter param = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param.Value = invoiceId;

            return m_InvoiceDAO.ExecuteUpdate("DeleteInvoice", param);
        }

        public DataRow AddNewRow()
        {
            return m_InvoiceDAO.NewRow();
        }

        public void AddNewInvoice(DataRow m_Row)
        {
            m_InvoiceDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_InvoiceDAO.ExecuteNoneQuery() > 0;
        }
    }
}
